package com.hdl.school.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hdl.school.bean.SerialMessage;
import com.hdl.school.schoolcontroller.R;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SerialMessageAdapter extends RecyclerView.Adapter<SerialMessageAdapter.SerialMessageViewHolder> {
    private List<SerialMessage> serialMessageList;
    private LayoutInflater inflater;

    public SerialMessageAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        serialMessageList=new ArrayList<>();
    }

    @NonNull
    @Override
    public SerialMessageAdapter.SerialMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.layout_serial_message, parent, false);
        return new SerialMessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SerialMessageAdapter.SerialMessageViewHolder holder, int position) {
        SerialMessage serialMessage=serialMessageList.get(position);
        holder.serialMessageTime.setText("["+serialMessage.getTime()+"]");
        holder.serialMessageContent.setText(serialMessage.getMessage());
        holder.serialMessageDirection.setText("["+serialMessage.getDirection()+"]");
        holder.serialMessagePort.setText("["+serialMessage.getPort()+"]");
        if(serialMessage.getDirection().equals("TX")){
            holder.serialMessageContent.setTextColor(Color.parseColor("#FF0033"));
            holder.serialMessageDirection.setTextColor(Color.parseColor("#FF0033"));
        }else if(serialMessage.getDirection().equals("RX")){
            holder.serialMessageContent.setTextColor(Color.parseColor("#66FF33"));
            holder.serialMessageDirection.setTextColor(Color.parseColor("#66FF33"));
        }else if(serialMessage.getDirection().equals("SYS")){
            holder.serialMessageContent.setTextColor(Color.parseColor("#FF9900"));
            holder.serialMessageDirection.setTextColor(Color.parseColor("#FF9900"));
        }else if(serialMessage.getDirection().equals("IN")){
            holder.serialMessageContent.setTextColor(Color.parseColor("#990099"));
            holder.serialMessageDirection.setTextColor(Color.parseColor("#990099"));
        }else if(serialMessage.getDirection().equals("OUT")){
            holder.serialMessageContent.setTextColor(Color.parseColor("#000099"));
            holder.serialMessageDirection.setTextColor(Color.parseColor("#000099"));
        }
    }

    @Override
    public int getItemCount() {
        return serialMessageList.size();
    }


    public void addItem(SerialMessage serialMessage){
        if(serialMessageList.size()>1000){
            serialMessageList.remove(0);
        }
        serialMessageList.add(serialMessage);
        notifyDataSetChanged();

    }

    public void clearItems(){
        serialMessageList.clear();
        notifyDataSetChanged();
    }

    class  SerialMessageViewHolder extends RecyclerView.ViewHolder{
        private TextView serialMessageTime;
        private TextView serialMessageContent;
        private TextView serialMessagePort;
        private TextView serialMessageDirection;

        public SerialMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            serialMessageTime=itemView.findViewById(R.id.serial_message_time);
            serialMessageContent=itemView.findViewById(R.id.serial_message_content);
            serialMessagePort=itemView.findViewById(R.id.serial_message_port);
            serialMessageDirection=itemView.findViewById(R.id.serial_message_direction);
        }
    }
}
